var param_platform_code = $.url().param('platform_code');
var param_type_code = $.url().param('type_code');
var param_date = $.url().param('date');

getLeagueInfo(param_platform_code, param_type_code, param_date);

function getLeagueInfo(platform_code, type_code, date) {
    $("#league_loading_zone").show();
    $("#league_data_zone").hide();
    $("#league_error_zone").hide();
    var token = getCookie('csrftoken');
    $.ajax({
        // url: '/football/get_league_info/',
        url: 'http://8.134.215.241:9080/jyb/core/plat/acquireplattypeleas',
        type: 'POST',
        headers:{'X-CSRFToken': token, "Content-Type": "application/json"},
        data: JSON.stringify({'baseDate': date, 'platCode': platform_code, 'typeCode': type_code}),
        success: function(data) {
            let jsonData = data['result']['platTypeLeaVoList'];
            console.log(jsonData);
            let listHtml = "";
            listHtml = listHtml + '<div class="self_common_zone">';
            jsonData.forEach((item, index) => {
                let league_code = item['leaCode'];
                let league_name = item['leaName'];
                listHtml = listHtml + '<div data-date="' + date + '" data-platform="' + platform_code + '" data-type="' + type_code + '" data-league="' + league_code + '" onclick="goToMatchPage(this)" class="self_common_item self_common_item_size_normal self_letter_spacing_2">';
                listHtml = listHtml + '<span>';
                listHtml = listHtml + league_name;
                listHtml = listHtml + '</span>';
                listHtml = listHtml + '</div>';
            });
            listHtml = listHtml + '</div>';
            $("#league_data_zone").html(listHtml);
            $("#league_loading_zone").hide();
            $("#league_error_zone").hide();
            $("#league_data_zone").show();
        },
        error: function() {
            $("#league_loading_zone").hide();
            $("#league_data_zone").hide();
            $("#league_error_zone").show();
        }
    });
}

function goToMatchPage(element) {
    const platform_code = element.dataset.platform;
    const type_code = element.dataset.type;
    const league_code = element.dataset.league;
    const date = element.dataset.date;
    goToOtherPage('./match.html?platform_code=' + platform_code + '&type_code=' + type_code + '&league_code=' + league_code + '&date=' + date );
}

function navigateToPlatformPage() {
    goToOtherPage('./platform.html?date=' + param_date );
}

function navigateToTypePage() {
    goToOtherPage('./type.html?platform_code=' + param_platform_code + '&date=' + param_date );
}